package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;
    //查询物资列表

    //categorys: [],

    //物质类别：加载分类数据，返回分页数据
    // queryMap: { pageNum: 1, pageSize: 5 },
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> getCategoryList(@RequestParam(value = "pageNum",required = false) Integer pageNum,
                                                                           @RequestParam(value = "pageSize",required = false) Integer pageSize){
        PageVO<ProductCategoryTreeNodeVO> pageVO = productCategoryService.getCategoryList(pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }

    //物质类别：添加分类：1.加载父类数据  2.添加
    //1.加载父级分类数据
    //返回值：parentCategorys: list[],
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryList(){
        List<ProductCategoryTreeNodeVO> parentCategoryList =  productCategoryService.getParentCategoryList();
        return ResponseBean.success(parentCategoryList);
    }

    //2.添加
    //参数：前端传来的对象ProductCategoryVO类对象
    //返回值：无返回值
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        productCategoryService.add(productCategoryVO);
        return ResponseBean.success();
    }

    //物质类别：打开编辑分类界面
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
      ProductCategoryVO productCategoryVO =  productCategoryService.edit(id);
        return ResponseBean.success(productCategoryVO);
    }

    //物质类别：编辑分类界面修改数据
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody ProductCategoryVO productCategoryVO){
        productCategoryService.update(id,productCategoryVO);
        return ResponseBean.success();
    }

    //物质类别：删除分类
    @DeleteMapping("/delete/{id}")
    public ResponseBean<Object> delete(@PathVariable Long id){
        productCategoryService.delete(id);
        return ResponseBean.success();
    }



}
